/******************************************************************************
 * Student Name: Ofri Keidar
 * Exercise Name: Ex2
 * File Description: Class Course's implementation
 ******************************************************************************/

#include "Course.h"
using namespace std;

//////////////////////////////////////////////////////////////////// public methods

/***************************************************************************************
 * function name: Course(const int courseNumber, const Lecturer& lecturer, const StudentLinkedList& studentDataBase)
 * The Input: New course's attributes- number, lecturer and student data base (list of
* 			  all students)
 * The output: None
 * The Function operation:  Creates a new course with given attributes.The student
 * 							database is used to check if given students exists, and if
 * 							so can be added to the course
 ***************************************************************************************/
Course::Course(const int courseNumber, const Lecturer* lecturer, const StudentLinkedList& studentDataBase):
		m_courseNumber(courseNumber), m_lecturer(lecturer), m_studentDataBase(studentDataBase) {
	m_studentInCourse = new StudentLinkedList(); // allocate a new student linked list
}

/***************************************************************************************
 * function name: ~Course()
 * The Input: None
 * The output: None
 * The Function operation: Set Lecturer pointer as NULL. Registered students must not be
 * 						   deleted,hence each node's student data is set as NULL.
 * 						   However, the list of registered students is deleted
 ***************************************************************************************/
Course::~Course() {

	// stop pointing at course's lecturer and students

	m_lecturer = NULL;

	// scan students list and make each student pointer point at NULL
	m_studentInCourse->initCurrent(); // initialize students' list pointer
	bool notEndOfList = m_studentInCourse->incrmntCurrent(); // move to next node
	while (notEndOfList == true) {
		m_studentInCourse->setCurrentStudentPointer(NULL); // current node points at NULL
		notEndOfList = m_studentInCourse->incrmntCurrent(); // move to next node
	}

	delete m_studentInCourse; // delete allocated memory
}

/***************************************************************************************
 * function name: addStudent(const int numOfStudents, int** idList)
 * The Input: Number of students to add and a list of their id's
 * The output: Number of students who were not added
 * The Function operation: Checks if each id on given list:
 * 						   Searches the id in student database to check if student exist,
 * 						   if so- continues, if not- False result is set to this id on
 * 						   the id list.
 * 						   If id exists in database, the method searches this id in
 * 						   registered students list. If it appears, student cannot be
 * 						   added again and False result is set to this id on the id list.
 *                         If the id does not appear, registered students list's adding
 *                         method is used in order to add the id to the list.
 *                         Return number of students who were not added
 ***************************************************************************************/
int Course::addStudent(const int numOfStudents, int** idList) {

	// for each ID in given array idList, check if such ID exist in student database or already registered to the course
	Student* canAddStudent;
	int mismatchCtr = 0; // counts how many ID's could not be added to course
	for (int i = 0; i < numOfStudents; ++i) {

		// check if student exists in database
		canAddStudent = m_studentDataBase.searchByKey(idList[i][0]);

		if (canAddStudent != NULL) { // current student exists

			// check if current student is already registered to the course
			if (m_studentInCourse->searchByKey(idList[i][0]) == NULL) { // student exists and not registered
				m_studentInCourse->addNode(canAddStudent); // add student
				idList[i][1] = true; //SUCCESS; // ignore current student in idList as coming back to menu

			} else { // current student is already registered
				idList[i][1] = false; //FAIL;
				++mismatchCtr; // increment mismatch counter
			}

		} else { // current student does not exist
			idList[i][1] = false; //FAIL; // student does not exist
			++mismatchCtr; // increment mismatch counter
		}

	} // for (int i = 0; i < numOfStudents; ++i)
	return mismatchCtr;
}
